package com.hl.sfzy.interceptor;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hl.sfzy.utils.R;
import org.springframework.lang.Nullable;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.function.ServerResponse;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;


public class MyIntercepter implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse
            response, Object handler) throws Exception {

        String token = request.getHeader("token");
        System.out.println(request.getHeader("Token"));
//        if(token==null||token.isEmpty()){
////            System.out.println(token);
//            ServletOutputStream outputStream = response.getOutputStream();
//            response.setCharacterEncoding("UTF-8");
//            response.setContentType("application/json;charset=utf-8");
//            R r=R.fail("你还没有登录");
//            r.setCode(401);
//            new ObjectMapper().writeValue(outputStream,r);
//            return false;
//        }
        return true;
    }
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse
            response, Object handler, @Nullable ModelAndView modelAndView) throws Exception
    {
        System.out.println("controller处理后，需要做的处理");
    }
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse
            response, Object handler, @Nullable Exception ex) throws Exception {
        System.out.println("最后处理");
    }
}
